//
//  YLPersonaInformationController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/1.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLPersonaInformationController.h"
#import "YLTitleArrowNoSubCell.h"
#import "YLTitleAllowCell.h"
#import "YLTitleIamgeListCell.h"
#import "YLPersonalnformationViewModel.h"
#import "YLGlobeHeader.h"
#import "YLSecurityPasswordViewController.h"
#import "WHNavBackButton.h"
#import "YLEditAdressViewController.h"
#import "YLMyInfoModel.h"
#import "YLAdressManagerViewController.h"
@interface YLPersonaInformationController ()
@property (nonatomic, strong) YLPersonalnformationViewModel *viewModel;

@end

@implementation YLPersonaInformationController

- (YLPersonalnformationViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLPersonalnformationViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人信息";
    
    self.viewModel.model = self.model;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleArrowNoSubCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleArrowNoSubCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleAllowCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleAllowCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLTitleIamgeListCell class]) bundle:nil] forCellReuseIdentifier:[YLTitleIamgeListCell cellID]];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editCity:) name:@"YLEditAdressNoti" object:nil];
    
    [self bindSingal];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.viewModel loadData];
    [self.tableView reloadData];
    
}


- (void)editCity:(NSNotification *)noti
{
    NSString *city = noti.userInfo[@"city"];
    self.viewModel.showCity = city;
    self.model.defaulReceiveAddress = city;
    [self.tableView reloadData];
    
}


- (void)bindSingal
{
    __weak typeof(self) weakSelf = self;
    // 添加地址
    [self.viewModel.addAdressSubject subscribeNext:^(id  _Nullable x) {
        YLEditAdressViewController *addAress = [[YLEditAdressViewController alloc] init];
        addAress.addAdressBlock = ^(NSString *city) {
            weakSelf.viewModel.showCity = city;
            weakSelf.model.defaulReceiveAddress = city;
            [weakSelf.tableView reloadData];
        };
        [weakSelf.navigationController pushViewController:addAress animated:YES];
    }];
    
    // 地址管理
    [self.viewModel.adressManagerSubject subscribeNext:^(id  _Nullable x) {
        YLAdressManagerViewController *adressManager = [[YLAdressManagerViewController alloc] init];
        [weakSelf.navigationController pushViewController:adressManager animated:YES];
    }];
    
    
    
    [self.viewModel.setSecurityPasswordSubject subscribeNext:^(id  _Nullable x) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            YLSecurityPasswordViewController *spCon = [[YLSecurityPasswordViewController alloc] init];
            [weakSelf.navigationController pushViewController:spCon animated:YES];
        }];
        
        
        NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"我们将向您的手机号182****1234手机发送一条验证码,确认是本人操作,是否同意"  attributes:@{NSForegroundColorAttributeName : YLColor(70, 70, 70) , NSFontAttributeName : [UIFont systemFontOfSize:14.0f]}];
        
        [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
        
        [okAction setValue:YLMainColor forKey:@"_titleTextColor"];
        
        UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        
        [noAction setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
        
        [alertController addAction:noAction];
        [alertController addAction:okAction];
        
        [weakSelf presentViewController:alertController animated:YES completion:nil];
    }];
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    YLIconTitleModelGroup *group = self.viewModel.groups[section];
    return group.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLIconTitleModelGroup *group = self.viewModel.groups[indexPath.section];
    YLIconTitleModel *model = group.models[indexPath.row];
    
    if ([model isKindOfClass:[YLIconTitleContentModel class]]) {
        YLTitleAllowCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleAllowCell cellID]];
        cell.contentModel = (YLIconTitleContentModel *)model;
        return cell;
    }
    
    if ([model isKindOfClass:[YLIconTitleNoArrowModel class]]) {
        YLTitleIamgeListCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleIamgeListCell cellID]];
        cell.iconTitleModel = (YLIconTitleNoArrowModel *)model;
        return cell;
    }
    
    YLTitleArrowNoSubCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLTitleArrowNoSubCell cellID]];
    
    cell.hiddenLine = YES;
    
    
    cell.iconTitleModel = model;
    return cell;
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return YLScreenW * 24 / 750.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [YLTitleArrowNoSubCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLIconTitleModelGroup *group = self.viewModel.groups[indexPath.section];
    YLIconTitleModel *model = group.models[indexPath.row];
    if (model.clickBlock) {
        NSString *name = model.clickBlock();
        if (name) {
            [self.navigationController pushViewController:[[NSClassFromString(name) alloc] init] animated:YES];
        }
        
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
